package cn.tx.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import cn.tx.model.Emp;
import cn.tx.model.OrderModel;
import cn.tx.model.Store;
import cn.tx.query.OrderModelQuery;
import cn.tx.query.StoreQuery;
import cn.tx.service.OrderModelService;
import cn.tx.service.StoreService;
import cn.tx.util.JSONWriter;
import cn.tx.util.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class StoreAction extends BaseAction implements ModelDriven<Store>{
	
	private Store store = new Store();
	
	private StoreService storeService;
	
	private OrderModelService orderModelService;
	
	private OrderModel orderModel = new OrderModel();
	
	private Integer productId;
	
	private Integer surplus;
	
	private Integer inStockNum;
	
	private Integer orderDetailId;
	
	

	public Integer getOrderDetailId() {
		return orderDetailId;
	}

	public void setOrderDetailId(Integer orderDetailId) {
		this.orderDetailId = orderDetailId;
	}

	public Integer getInStockNum() {
		return inStockNum;
	}

	public void setInStockNum(Integer inStockNum) {
		this.inStockNum = inStockNum;
	}

	public Integer getProductId() {
		return productId;
	}

	public void setProductId(Integer productId) {
		this.productId = productId;
	}

	public Integer getSurplus() {
		return surplus;
	}

	public void setSurplus(Integer surplus) {
		this.surplus = surplus;
	}

	public OrderModel getOrderModel() {
		return orderModel;
	}

	public void setOrderModel(OrderModel orderModel) {
		this.orderModel = orderModel;
	}

	public OrderModelService getOrderModelService() {
		return orderModelService;
	}

	public void setOrderModelService(OrderModelService orderModelService) {
		this.orderModelService = orderModelService;
	}

	public Store getStore() {
		return store;
	}

	public void setStore(Store store) {
		this.store = store;
	}

	/**
	 * 查询对象
	 */
	private StoreQuery erpQuery = new StoreQuery();
	
	
	public StoreService getStoreService() {
		return storeService;
	}

	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}
	

	public StoreQuery getErpQuery() {
		return erpQuery;
	}

	public void setErpQuery(StoreQuery erpQuery) {
		this.erpQuery = erpQuery;
	}

	
	public void store_inStock(){
		
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session2 = context.getSession();
		Emp emp = (Emp) session2.get("user");
		storeService.updateInStock(emp, orderDetailId, productId, inStockNum, store.getStoreId());
		JSONWriter.writeString("yes", response);
	}
	/**
	 * 查询员工的列表
	 * @return
	 */
	public String store_sdlist(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		Page page = storeService.selectObjectByConditionPage(erpQuery, exclude);
		context.put("page", page);
		
		return SUCCESS;
	}
	
	
	public String store_inList(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		String orderTypeStr = erpQuery.getOrderTypeStr();
		//把逗号分隔的字符串拆分出来
		if(StringUtils.isNotBlank(orderTypeStr)){
			String[] split = orderTypeStr.split(",");
			List<Integer> ots = new ArrayList<Integer>();
			for(String str : split){
				ots.add(new Integer(str));
			}
			//把多个订单类型的数据设置上
			erpQuery.setOrderTypes(ots);
			
		}
		exclude.add("orderTypeStr");
		OrderModelQuery omq = new OrderModelQuery();
		try {
			BeanUtils.copyProperties(omq, erpQuery);
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		Page page = orderModelService.selectObjectByConditionPage(omq, exclude);
		context.put("page", page);
		return SUCCESS;
	}
	
	public String store_inDetail(){
		orderModel = orderModelService.getObjectById(orderModel.getOrderId());
		return SUCCESS;
	}
	
	
	
	
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	public String store_input(){
		
		return SUCCESS;
	}
	/**
	 * 跳转到修改页面
	 * @return
	 */
	public String store_preupdate(){
		store = storeService.getObjectById(store.getStoreId());
		return SUCCESS;
	}
	
	
	public String store_chastore(){
		ActionContext context = ActionContext.getContext();
		List<Store> slist = storeService.listAll();
		context.put("slist", slist);
		return SUCCESS;
	}
	/**
	 * 保存
	 * @return
	 */
	public String store_save(){
		storeService.save(store);
		return TOLIST;
	}
	/**
	 * 修改
	 * @return
	 */
	public String store_update(){
		storeService.update(store);
		return TOLIST;
	}
	/**
	 * 修改
	 * @return
	 */
	public String store_delete(){
		store = storeService.getObjectById(store.getStoreId());
		storeService.delete(store);
		return TOLIST;
	}

	@Override
	public Store getModel() {
		return store;
	}
	
	
	
	

}
